var imageUrl;
function imageUpload(file) {
    if (file.size > 2 * 1024 * 1024) { // 2MB size limit
        console.log("文件大小超过2MB");
        return;
    }
    let image = new FormData()
    image.append('file', file, 'image.png')
    axios.post("/group1/upload?output=json&scene=&path=", image, {
        headers: {
            'Content-Type': 'multipart/form-data',
        }
    })
        .then(function (response) {
            imageUrl = response.data.path
            copyToClickBoard(imageUrl);
            console.log(imageUrl);
        })
        .catch(function (error) {
            console.log("请求失败");
            console.log(error);
        })
}

setPasteImg();
//获取粘贴板上的图片
function setPasteImg() {
    //粘贴事件
    document.addEventListener('paste', function (event) {
        if (event.clipboardData || event.originalEvent) {
            var clipboardData = (event.clipboardData || event.originalEvent.clipboardData);
            if (clipboardData.items) {
                var blob;
                for (var i = 0; i < clipboardData.items.length; i++) {
                    if (clipboardData.items[i].type.indexOf("image") !== -1) {
                        blob = clipboardData.items[i].getAsFile();
                    }
                }
                if (blob && document.getElementsByClassName("CodeMirror-activeline")) {
                    imageUpload(blob);
                }
            }

        }

    })
}
function copyToClickBoard(content) {
    var text = '![](' + content + ')';
    if (!navigator.clipboard) {
        var ele = document.createElement("input");
        ele.value = text;
        document.body.appendChild(ele);
        ele.select();
        document.execCommand("copy");
        document.body.removeChild(ele);
        if (document.execCommand("copy")) {
            console.log("复制成功1");
            djangoEditor.insertValue(text);
            djangoEditor.focus();
        } else {
            console.log("复制失败1！");
        }
    } else {
        navigator.clipboard.writeText(text)
            .then(() => {
                console.log("粘贴图片成功");
                djangoEditor.insertValue(text);
                djangoEditor.focus();
            })
            .catch(err => {
                console.log('粘贴图片,上传失败', err);
            })
    }
}